import copy
import cv2 as cv
import numpy as np


class SecondWeekDemo5:
    width = 1200
    img = None
    gray = None

    def __init__(self, img_path):
        self.img = cv.imread(img_path)
        self.gray = cv.cvtColor(self.img, cv.COLOR_BGR2GRAY)

    def show(self, name, img):
        cv.namedWindow(name, 0)
        cv.resizeWindow(name, self.width, self.width)
        cv.imshow(name, img)

    def image(self):
        return copy.deepcopy(self.img)

    def showImage(self):
        self.show("img", self.img)

    def grayImage(self):
        return copy.deepcopy(self.gray)

    def sift(self):
        # SIFT
        img1 = self.image()
        gray = self.grayImage()
        sift = cv.xfeatures2d.SIFT_create()
        keyPoint1 = sift.detect(img1)
        sift.compute(gray, keyPoint1)
        cv.drawKeypoints(img1, keyPoint1, img1, [0, 0xff, 0])
        self.show("sift", img1)

    def fast(self):
        # FAST
        img2 = self.image()
        gray = self.grayImage()
        fast = cv.FastFeatureDetector_create()
        keyPoint2 = fast.detect(gray)
        cv.drawKeypoints(img2, keyPoint2, img2, [0, 0xff, 0])
        self.show("fast", img2)

    def orb(self):
        # ORB
        img3 = self.image()
        gray = self.grayImage()
        orb = cv.ORB_create()
        keyPoint3 = orb.detect(gray)
        cv.drawKeypoints(img3, keyPoint3, img3, [0, 0xff, 0])
        self.show("orb", img3)

    def harris(self):
        # Harris
        img = self.image()
        gray = self.grayImage()
        gray32 = np.float32(gray)
        img4 = cv.cornerHarris(gray32, 2, 3, 0.04)
        img5 = cv.dilate(img4, None)
        img[img5 > 0.01 * img5.max()] = [0, 0, 255]
        self.show("harris", img)

    def showAll(self):
        self.showImage()
        self.sift()
        self.fast()
        self.orb()
        self.harris()
        cv.waitKey()
        cv.destroyAllWindows()


chessboard = SecondWeekDemo5("chessboard.png")
chessboard.showAll()
home = SecondWeekDemo5("home.jpg")
home.showAll()
